package com.arron.algorithm.leetcodetop100.数组.矩阵;

import java.util.ArrayList;
import java.util.List;

public class 螺旋矩阵 {


    public List<Integer> spiralOrder(int[][] matrix) {

        int up_bound = 0;
        int down_bound = matrix.length-1;
        int left_bound = 0;
        int right_bound = matrix[0].length-1;

        List<Integer> res = new ArrayList<>();
        while (up_bound <= down_bound && left_bound<=right_bound) {

            //上边，从左到右遍历
            for (int i = left_bound; i <= right_bound; i++) {

                res.add(matrix[up_bound][i]);

            }
            up_bound++;
            if (up_bound>down_bound){
                break;
            }
            // 右边 从上到下
            for (int i = up_bound; i <= down_bound; i++) {
                res.add(matrix[i][right_bound]);
            }
            right_bound--;
            if (left_bound>right_bound){
                break;
            }
            // 下边 从右到左
            for (int i = right_bound; i >= left_bound; i--) {
                res.add(matrix[down_bound][i]);
            }
            down_bound--;
            if (up_bound>down_bound){
                break;
            }
            // 左边 从下到上
            for (int i = down_bound; i >= up_bound; i--) {
                res.add(matrix[i][left_bound]);
            }
            left_bound++;
        }
        return res;
    }

}
